package www.nearby.ren;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ogaclejapan.smarttablayout.SmartTabLayout;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItemAdapter;
import com.ogaclejapan.smarttablayout.utils.v4.FragmentPagerItems;


public class ANearbyMain extends AppCompatActivity implements ViewPager.OnPageChangeListener {


    public SmartTabLayout tabLayout;
    public NearbyViewPager nearbyViewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anearby_main);
        initview();

    }
    public void initview(){
        tabLayout =(SmartTabLayout) findViewById(R.id.tabLayout);
        nearbyViewPager =(NearbyViewPager) findViewById(R.id.viewPager);
        FragmentPagerItems pages = FragmentPagerItems.with(this)
                .add(R.string.nearby_fragment_home, NearbyFragmentHome.class)
                .add(R.string.nearby_fragment_message, NearbyFragmentMessage.class)
                .add(R.string.nearby_fragment_me, NearbyFragmentMe.class)
                .create();
        FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
                getSupportFragmentManager(),
                pages);
        nearbyViewPager.setOffscreenPageLimit(pages.size());
        nearbyViewPager.setAdapter(adapter);
        nearbyViewPager.setScanScroll(true);
        nearbyViewPager.addOnPageChangeListener(this);
        setup(tabLayout);
        tabLayout.setViewPager(nearbyViewPager);
    }
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
    //添加tab图标
    public void setup(SmartTabLayout layout) {
        layout.setCustomTabView(new SmartTabLayout.TabProvider() {
            @Override
            public View createTabView(ViewGroup container, int position, PagerAdapter adapter) {
                View view = LayoutInflater.from(ANearbyMain.this).inflate(R.layout.main_menu_layout, container, false);
                ImageView icon = (ImageView) view.findViewById(R.id.icon);
                TextView name = (TextView) view.findViewById(R.id.name);
                name.setText(adapter.getPageTitle(position));
                switch (position) {
                    case 0:
                        icon.setImageDrawable(getResources().getDrawable(R.drawable.nearby_home_menu_selector));
                        break;
                    case 1:

                        icon.setImageDrawable(getResources().getDrawable(R.drawable.nearby_message_menu_selector));
                        break;
                    case 2:
                        icon.setImageDrawable(getResources().getDrawable(R.drawable.nearby_me_menu_selector));
                        break;
                    default:
                        throw new IllegalStateException("Invalid position: " + position);
                }
                return view;
            }
        });

    }
}
